package com.myBlog.utils;

import com.alibaba.fastjson2.JSONObject;

import java.util.Objects;

public class JsonUtil {

    private static int Code_ok = 1;
    private static int Code_ERR = 0;

    public static JSONObject ok_info(JSONObject info){
        JSONObject result = new JSONObject();
        result.put("info", Objects.requireNonNullElse(info, "{}"));
        result.put("success", true);
        result.put("status", Code_ok);

        return result;
    }

    public static JSONObject ok_info_withDefault(){
        return JsonUtil.ok_info("操作成功");
    }
    public static JSONObject ok_info(String info){
        JSONObject result = new JSONObject();
        result.put("info", Objects.requireNonNullElse(info, "操作成功"));
        result.put("success", true);
        result.put("status", Code_ok);

        return result;
    }

    public static JSONObject ok_with_Object(String key, Object value){
        JSONObject result = new JSONObject();
        result.put( key, value );
        result.put("success", true);
        result.put("status", Code_ok);

        return result;
    }

    public static JSONObject ok_with_Data(Object value){
        return ok_with_Object("data", value);
    }

    public static JSONObject error_info(String err){
        JSONObject result = new JSONObject();
        result.put("info", Objects.requireNonNullElse(err, "未知异常"));
        result.put("success", false);
        result.put("status", Code_ERR);

        return result;
    }

}
